<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="d" uri="http://dgg.net/jsp/jstl/core" %>

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>待分配</title>
    <c:import url="../../header.jsp"/>
    <script> var ALLOT_REFUSED = '${ALLOT_REFUSED}';</script>
</head>
<body>
<div class="body-main">
    <div class="main-title">
				<span class="bus-state">
					<font>待分配</font>
				</span>
        <div class="title-tab">
            <a href="${basePath}/sc/distribution/waitAllotScOrder.html?status=${ALL}" <c:if test="${status eq 'all'}"> class="active"</c:if>>全部</a>
            <a href="${basePath}/sc/distribution/waitAllotScOrder.html?status=${ALLOT_WAITING}" <c:if test="${status==ALLOT_WAITING}"> class="active"</c:if>">新增待分配</a>
            <a href="${basePath}/sc/distribution/waitAllotScOrder.html?status=${ALLOT_REFUSED}" <c:if test="${status==ALLOT_REFUSED}"> class="active"</c:if>>分配退回</a>
        </div>
    </div>
    <form action="" id="waitAllotForm">
        <input type="hidden" id="statusArr" name="statusArr" value="${status}">
        <input type="hidden" id="timeOutStatus" name="timeOutStatus" value="${timeOutStatus}">
        <div class="titleSearch">
            <div class="layui-inline">
                <label>客户电话：</label>
                <div class="layui-input-inline">
                    <input name="customerPhone" type="text" class="input-style" maxlength="50"
                           placeholder="请输入客户电话">
                </div>
            </div>
            <div class="layui-inline">
                <label>订单来源：</label>
                <div class="layui-input-inline">
                    <select class="select-style" id="signOrgId" name="signOrgId">
                        <option value>请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label>生产所属事业部：</label>
                <div class="layui-input-inline">
                    <select class="select-style" id="productOrgId" name="productOrgId">
                        <option value>请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label>业务类型：</label>
                <div class="layui-input-inline">
                    <div id="business_typeCode">
                        <input type="hidden" name="businessType" class="plug-in-msg"/>
                        <input type="hidden" name="typeCode" id="typeCode" class="plug-in-code"/>
                        <input type="text" readonly placeholder="请选择业态" class="chose-plugIn layui-input"
                               name="businessTypeName">
                        <div class="bus-picker">
                            <div class="bus-picker-tab">
                                <a href="javascript:void(0);" class="active">请选择业态</a>
                            </div>
                            <div class="bus-picker-content"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-inline">
                <label>下单时间：</label>
                <div class="layui-inline">
                    <input type="text" name="orderTimeStart" placeholder="开始时间" class="Wdate input-style" id="d4311"
                           onFocus="var d4312=$dp.$('d4312'); WdatePicker({onpicked:function(){d4312.focus();}, maxDate:'#F{ $dp.$D(\'d4312\')}', dateFmt:'yyyy-MM-dd'})">
                </div>
                <span>~</span>
                <div class="layui-inline">
                    <input type="text" name="orderTimeEnd" placeholder="结束时间" class="Wdate input-style" id="d4312"
                           onFocus="WdatePicker({minDate:'#F{$dp.$D(\'d4311\')}', dateFmt:'yyyy-MM-dd'})">
                </div>
            </div>
            <div class="layui-inline">
                <label>关键字：</label>
                <div class="layui-input-inline">
                    <input name="searchKey" type="text" class="input-style" maxlength="50" style="width:250px"
                           placeholder="客户名/订单编号/生产订单编号/合同编号">
                </div>
            </div>

            <div class="layui-inline">
                <button class="layui-btn layui-btn-mini layui-btn-normal" type="button" onclick="waitAllotDataTableReload()">搜索</button>
                <button class="layui-btn layui-btn-mini layui-btn-primary" type="button" onclick="cleanSearch()">清空</button>
                <d:hasUrlAuth authCode="ZZSC_WAITALLOT_EXPORT">
                    <button class="layui-btn layui-btn-mini layui-btn-green" type="button" onclick="export_data()">导出</button>
                </d:hasUrlAuth>
            </div>
        </div>
    </form>
    <div class="table-conter">

        <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="callPhoneByProductionPage()">打电话</a>
        <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="openAllotPage()">分配</a>
        <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="openAllotListPage()">批量分配</a>
        <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="certificateMatchPage()">证书匹配</a>
        <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="returnOrderPage()">退单</a>
        <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="openremarkPage()">备注</a>
        <div class="fr">
            <span class="bgGreen">分配超期</span>
        </div>
    </div>
    <div class="table-body">
        <!--表格 start-->
        <table id="waitAllotScOrderTable" class="cell-border crm-table columnsHidden dataTable">
            <thead>
            <tr>
                <th width="20"><input type="checkbox" class="checkall"/></th>
                <th width="50">序号</th>
                <th>生产事业部</th>
                <th>订单编号</th>
                <th>生产订单编号</th>
                <th>合同编号</th>
                <th>客户</th>
                <th>业务类型</th>
                <th>订单产品</th>
                <th>销售订单备注</th>
                <th>下单时间</th>
                <th>签单商务</th>
                <th>签单部门</th>
                <th>订单来源</th>
                <th>合同金额</th>
                <th>合同周期</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
        <!--表格 end-->
    </div>
</div>

<c:import url="/WEB-INF/jsp/footer.jsp"/>
<script type="text/javascript">
    var NORMAL = '${NORMAL}', TIME_OUT = '${TIME_OUT}';
</script>
<script src="${rootUrl}/static/js/zzsc/credential/credentailCommon/credentailCommon.js?v=${version}"></script>
<script type="text/javascript" src="${basePath}/static/js/zzsc/production/sc_common.js?v=${version}"></script>
<script type="text/javascript" src="${basePath}/static/js/zzsc/common/call_common.js?v=${version}"></script>
<script type="text/javascript" src="${basePath}/static/js/zzsc/inprocess/inprocess_list.js?v=${version}"></script>
<script type="text/javascript">
    Plug.plug_ins("post", "${basePath}/zzsc/common/tree_book_list.do?code=BUS_YT_ZZ&&type=2&&level=3", "#business_typeCode", '选择业态', '', true, 'BUS_YT_ZZ', '', '');
</script>
<script type="text/javascript">

    var waitAllotTableObj;
    syb("#signOrgId");//初始化事业部
    syb("#productOrgId");//初始化事业部
    // 分配
    function openAllotPage() {
        var dataArr = $('#waitAllotScOrderTable').dataTableExtend('getCheck');
        if (dataArr.length == 0) {
            DGG.Warning('请选择一条订单');
            return false;
        }
        if (dataArr.length > 1) {
            DGG.Warning('只能选择一条订单');
            return false;
        }
        openAllotScOrderPage(dataArr[0].id);
    }

    //批量分配
    function openAllotListPage() {
        var dataArr = $('#waitAllotScOrderTable').dataTableExtend('getCheck');
        if (dataArr.length === 0) {
            DGG.Warning('请至少选择一条订单');
            return false;
        }
        var idStr = '';
        var orgIds = [];
        for (var i = 0; i < dataArr.length; i++) {
            if (i > 0) {
                idStr = idStr + ',';
            }
            idStr = idStr + dataArr[i].id;
            orgIds.push(dataArr[i].productOrgId)
        }
        if(!isAllEqual(orgIds)){
            DGG.Warning('请选择相同生产所属事业部订单');
            return false;
        }
        DGG.loading();
        $.post(sysInfo.basePath + "/sc/distribution/allot_order_list_page.html", {idStr: idStr,productOrgId:orgIds[0]}, function (html) {
            DGG.Open({
                content: html,
                title: "订单分配",
                area: '500px',
                yes: function (index, layero) {
                    DGG.loading();
                    var allotWay = $("#allotWay").val();//获取分配方式
                    var flowUserId = $("#flowUserId").val();//获取分配人员
                    var contactStartTime = $("[name='contractStartTime']").val();//合同开始时间
                    var contractEndTime = $("[name='contractEndTime']").val();//合同结束时间
                    if (null == allotWay || '' == allotWay) {
                        DGG.Warning("请选择分配方式");
                        DGG.loading("close");
                        return false;
                    }
                    if (null == flowUserId || '' == flowUserId) {
                        DGG.Warning("请选择分配人员");
                        DGG.loading("close");
                        return false;
                    }
                    if (null == contactStartTime || '' == contactStartTime) {
                        DGG.Warning("请选择合同开始时间");
                        DGG.loading("close");
                        return false;
                    }
                    if (null == contractEndTime || '' == contractEndTime) {
                        DGG.Warning("请选择合同结束时间");
                        DGG.loading("close");
                        return false;
                    }
                    var param = {};
                    param.receiverId = flowUserId;
                    param.allotWay = allotWay;
                    param.idStr = idStr;
                    //协单人员
                    param.assistIds = getVal('#assistId', 0);
                    param.lastRemark = $("[name='lastRemark']").val();
                    param.isNeedDispose = $("[name='isNeedDispose']:checked").val();
                    param.contractStartDate = $("[name='contractStartTime']").val();
                    param.contractEndDate = $("[name='contractEndTime']").val();
                    $.post(sysInfo.basePath + "/sc/distribution/allot_order_list.do", param, function (result) {
                        if (result.code == 0) {
                            DGG.Succeed('分配成功');
                            waitAllotTableObj.ajax.reload();
                            layer.close(index);
                            DGG.loading("close");
                        } else {
                            DGG.Errors(result.msg);
                            DGG.loading("close");
                        }
                    }, 'json');

                }
            })

        }, 'html');
    }

    // 打电话
    function callPhoneByProductionPage() {
        var dataArr = $('#waitAllotScOrderTable').dataTableExtend('getCheck');
        if (dataArr.length == 0) {
            DGG.Warning('请选择一条订单')
            return false;
        }
        if (dataArr.length > 1) {
            DGG.Warning('只能选择一条订单');
            return false;
        }
        callPhoneByProduction(dataArr[0].id);
    }

    // 退单
    function returnOrderPage() {
        var dataArr = $('#waitAllotScOrderTable').dataTableExtend('getCheck');
        if (dataArr.length == 0) {
            DGG.Warning('请选择一条订单');
            return false;
        }
        if (dataArr.length > 1) {
            DGG.Warning('只能选择一条订单');
            return false;
        }
        chargeBackOrder("waitAllotScOrderTable", dataArr[0].id);
    }

    //搜索
    function waitAllotDataTableReload() {
        waitAllotTableObj.ajax.reload();
    }

    // 证书匹配
    function certificateMatchPage() {
        var dataArr = $('#waitAllotScOrderTable').dataTableExtend('getCheck');
        if (dataArr.length == 0) {
            DGG.Warning('请选择一条订单');
            return false;
        }
        if (dataArr.length > 1) {
            DGG.Warning('只能选择一条订单');
            return false;
        }
        certificateMatch("waitAllotScOrderTable", dataArr[0].id);
    }

    // 备注
    function openremarkPage() {
        var dataArr = $('#waitAllotScOrderTable').dataTableExtend('getCheck');
        if (dataArr.length == 0) {
            DGG.Warning('请选择一条订单');
            return false;
        }
        if (dataArr.length > 1) {
            DGG.Warning('只能选择一条订单');
            return false;
        }
        remarkAjaxSubmit(dataArr[0].id);
    }

    // 弹出分配页面
    function openAllotScOrderPage(id) {
        DGG.loading();
        $.post(sysInfo.basePath + "/sc/distribution/allotScOrderPage.html", {id: id}, function (html) {
            DGG.Open({
                content: html,
                title: "订单分配",
                area: '500px',
                success:function(layero,index){
                    $('.layerIndex2').val(index);
                },
                yes: function (index, layero) {
                    DGG.loading();
                    var allotWay = $("#allotWay").val();//获取分配方式
                    var flowUserId = $("#flowUserId").val();//获取分配人员
                    var contactStartTime = $("[name='contractStartTime']").val();//合同开始时间
                    var contractEndTime = $("[name='contractEndTime']").val();//合同结束时间
                    if (null == allotWay || '' == allotWay) {
                        DGG.Warning("请选择分配方式");
                        DGG.loading("close");
                        return false;
                    }
                    if (null == flowUserId || '' == flowUserId) {
                        DGG.Warning("请选择分配人员");
                        DGG.loading("close");
                        return false;
                    }
                    if (null == contactStartTime || '' == contactStartTime) {
                        DGG.Warning("请选择合同开始时间");
                        DGG.loading("close");
                        return false;
                    }
                    if (null == contractEndTime || '' == contractEndTime) {
                        DGG.Warning("请选择合同结束时间");
                        DGG.loading("close");
                        return false;
                    }
                    var param = {};
                    param.receiverId = flowUserId;
                    param.allotWay = allotWay;
                    param.id = id;
                    //协单人员
                    param.assistIds = getVal('#assistId', 0);
                    param.lastRemark = $("[name='lastRemark']").val();
                    param.isNeedDispose = $("[name='isNeedDispose']:checked").val();
                    param.contractStartDate = $("[name='contractStartTime']").val();
                    param.contractEndDate = $("[name='contractEndTime']").val();
                    $.post(sysInfo.basePath + "/sc/distribution/allotScOrder.do", param, function (result) {
                        if (result.code == 0) {
                            DGG.Succeed('分配成功');
                            waitAllotTableObj.ajax.reload();
                            layer.close(index);
                            DGG.loading("close");
                        } else {
                            DGG.Errors(result.msg);
                            DGG.loading("close");
                        }
                    }, 'json');

                }
            })

        }, 'html');
    }

    // 弹出退单页面
    function retuanOrderAjaxSubmit(id) {
        DGG.loading();
        $.post(sysInfo.basePath + "/sc/distribution/returnScOrderPage.html", {id: id}, function (html) {
            DGG.Open({
                content: html,
                title: "订单退单",
                area: '500px',
                yes: function (index, layero) {
                    DGG.loading();
                    var return_remark = $("#return_remark").val();
                    if (return_remark.length > 200) {
                        DGG.Warning("备注内容不能超过200字");
                        DGG.loading("close");
                        return false;
                    }
                    var param = $("#returnForm").serializeObject();//获取退单参数
                    param.id = id;
                    $.post(sysInfo.basePath + "/sc/distribution/returnOrder.do", param, function (result) {
                        if (result.code == 0) {
                            DGG.Succeed('退单成功');
                            waitAllotTableObj.ajax.reload();
                            layer.close(index);
                            DGG.loading("close");
                        } else {
                            DGG.Errors(result.msg);
                            DGG.loading("close");
                        }
                    }, 'json');

                }
            })

        }, 'html');
    }

    // 弹出备注页面
    function remarkAjaxSubmit(id) {
        DGG.loading();
        $.post(sysInfo.basePath + "/sc/distribution/remarkScOrderPage.html", {id: id}, function (html) {
            DGG.Open({
                content: html,
                title: "订单备注",
                area: '400px',
                yes: function (index, layero) {
                    var remark = $("#order_remark").val();
                    if (checkIsEmpty(remark)) {
                        DGG.Errors("备注不能为空!");
                        return;
                    }
                    DGG.loading();
                    var param = $("#remarkForm").serializeObject();//获取备注参数
                    param.id = id;
                    $.post(sysInfo.basePath + "/sc/distribution/remarkOrder.do", param, function (result) {
                        if (result.code == 0) {
                            DGG.Succeed('备注成功');
                            waitAllotTableObj.ajax.reload();
                            layer.close(index);
                            DGG.loading("close");
                        } else {
                            DGG.Errors(result.msg);
                            DGG.loading("close");
                        }
                    }, 'json');

                }
            })

        }, 'html');
    }

    function export_data() {
        export_table('/sc/distribution/export.do', getSearchData());
    }

    // 获取搜索数据
    function getSearchData() {
        return $("#waitAllotForm").serializeObject();//获取查询参数
    }

    $(document).ready(function () {
        //初始化dataTable列表
        //初始化dataTable列表
        waitAllotTableObj = $('#waitAllotScOrderTable').dataTableExtend({
            "ajax": {"url": "${basePath}/sc/distribution/queryPage.do", "type": "post"},
            "fnServerParams": function (aoData) {
                aoData = $.extend(aoData, $("#waitAllotForm").serializeObject());
                return aoData;
            },
            "scrollX": "true",//是否开启横项滚动
            "fixedColumns": {//锁定列，只有开启scrollX时才生效
                "leftColumns": 3,//左侧锁定2列
                "rightColumns": 1//右侧锁定1列
            },
            "aoColumns": [
                {
                    "orderable": false,
                    "data": "id",
                    "render": function (data, type, full, meta) {
                        return '<input type="checkbox"  name="ids" class="checkchild"  value="' + data + '" />';
                    }
                }
                , {
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return meta.row + 1;
                    }
                }, {
                    "orderable": false,
                    "data": "productOrgName"
                }, {
                    "orderable": false,
                    "data": "orderNo",
                    "render": function (data, type, full, meta) {
                        return ('<a href=\'javascript:void(0)\' ti  onclick="openBusinessDetail(\'' + full.orderNo + '\',\'' + full.orderId + '\')" /><span class="layui-table-link">' + data + '</span></a>&nbsp;');

                    }
                }, {
                    "orderable": false,
                    "data": "scProductOrderNo",
                    "render": function (data, type, full, meta) {
                        var result = "";
                        result += '<a href="javascript:;"  class="layui-table-link" onclick="openOrderInfoDetail(\'' + full.scProductOrderNo + '\',\'' + full.id + '\')" /><p>' + data + '</p></a>';
                        var reservationStatus = full.reservationStatus;
                        if (reservationStatus) {
                            var split = reservationStatus.split(',');
                            for (var i = 0; i < split.length; i++) {
                                var splitElement = split[i];
                                if ('PRODUCT_RESERVATION_STATUS_01' === splitElement) {//预约
                                    result += '<a href="javascript:;"   class="label label-yellow" onclick="certificateAppointment(\'' + full.id + '\')">' + '已预约' + '</a>';
                                } else if ('PRODUCT_RESERVATION_STATUS_02' === splitElement) {//预定
                                    result += '<a href="javascript:;"   class="label label-green" onclick="openCertificateMatch(\'' + full.id + '\')">' + '已预定' + '</a>';
                                }
                            }
                        }
                        if ('ZZSC_STATE_SCPROORDER_CODE14' == full.status) {//退回订单显示退回
                            result += '<a href="javascript:;"   class="label label-blue">' + '订单退回' + '</a>';
                        }
                        return result;
                    }
                },

                {
                    "orderable": false,
                    "data": "contractNo",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "orderable": false,
                    "data": "customerName",
                    "render": function (data, type, full, meta) {
                        return emptyStringConvert(full.customerName) + '<p>' + emptyStringConvert(full.customerPhone) + '</p>';
                    }
                },
                {
                    "orderable": false,
                    "data": "businessProductName",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "orderable": false,
                    "data": "productName",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "orderable": false,
                    "data": "orderRemark",
                    "render": function (data, type, full, meta) {
                        if(data){
                            return  '<div class="line2" style="min-width: 120px" title="'+data+'">'+data+'</div> ';
                        }else{
                            return '';
                        }
                    }
                },
                {
                    "orderable": true,
                    "data": "orderTime",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "orderable": false,
                    "data": "signUserName",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                }, {
                    "orderable": false,
                    "data": "signUserOrgName",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                }, {
                    "orderable": false,
                    "data": "signOrgName",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                }, {
                    "orderable": false,
                    "data": "money"
                },
                {
                    "orderable": false,
                    "data": "contractStartDate",
                    "render": function (data, type, r, meta) {
                        return data ? data.replace(new RegExp("-",'g'),"")+"-"+r.contractEndDate.replace(new RegExp("-",'g'),"") : '';
                    }
                },
                {
                    "orderable": false,
                    "data": "id",
                    "width": "190",
                    "render": function (d, type, r, meta) {
                        var result = "";
                        //打电话
                        result += '<a href=\'javascript:void(0)\' ti  onclick="callPhoneByProduction(\'' + r.id + '\')" /><span class="layui-table-link" style="font-weight: bold">打电话</a>&nbsp;&nbsp;&nbsp;';
                        result += '<a href=\'javascript:void(0)\' ti  onclick="remarkAjaxSubmit(\'' + r.id + '\')" /><span class="layui-table-link" style="font-weight: bold">备注</a>&nbsp;';
                        return result;
                    }
                }],
            "columnDefs": [{
                targets: [3], //第1，2，3列
                createdCell: function (td, cellData, rowData, row, col) {
                    if (null != rowData.distributionTimeOutStatus) {//这里优化为只有 下单待分配 才显示黄色
                        if (rowData.nodeTimeStatus == '${TIME_OUT}' && rowData.status == 'ZZSC_STATE_SCPROORDER_CODE1') {
                            $(td).parent().addClass("bgGreen");
                            return;
                        }
                    }
                }
            }, {
                targets: ["_all"], "defaultContent": ""
            }]
        });
    });

    /**
     * 判断数组原属是否全相同
     * @param array
     * @returns {boolean}
     */
    function isAllEqual(array) {
        if (array.length > 0) {
            return !array.some(function (value, index) {
                return value !== array[0];
            });
        } else {
            return true;
        }
    }
</script>
</body>

</html>